<?php
	include("../includes/connection.php");
	$gambar = mysql_fetch_array(mysql_query("SELECT code FROM tb_portfolio WHERE id='$_POST[id]'"));
	$sql = "DELETE FROM tb_portfolio WHERE id='$_POST[id]'";
	$query = mysql_query($sql);

	$sql_image = "SELECT * FROM tb_portfolio_image WHERE id_portfolio = '$_POST[id]'";
	$query_image = mysql_query($sql_image);
	while ($result_image = mysql_fetch_array($query_image)) {
		$code = $result_image["code"];
		unlink("../img/portfolio/".$result_image["code"]."/".$result_image["gambar"]);
		unlink("../img/portfolio/".$result_image["code"]."/thumbnail/".$result_image["gambar"]);
	}

	$sql_detail = "DELETE FROM tb_portfolio_image WHERE id_portfolio='$_POST[id]'";
	$query_detail = mysql_query($sql_detail);

	$dir = "../img/portfolio/".$code;
	$thumb_dir = "../img/portfolio/".$code."/thumbnail";

	if (is_dir($thumb_dir)) {
		$objects = scandir($thumb_dir);
		foreach ($objects as $object) {
			if ($object != "." && $object != "..") {
				unlink($thumb_dir."/".$object);
			}
		}
		reset($objects);
		rmdir($thumb_dir);
  }

	if (is_dir($dir)) {
		$objects = scandir($dir);
		foreach ($objects as $object) {
			if ($object != "." && $object != "..") {
				unlink($dir."/".$object);
			}
		}
		reset($objects);
		rmdir($dir);
  }
?>